<?php
include("utils.php");
if($_SESSION['siteuserId']==""){
    header("location:start.php");
}
if($_POST['mode']=="change_status"){
    change_status();
}
else{
    main_content("main();");
}

function main(){
    if($_SESSION['languageId']!=""){
        $languageId=$_SESSION['languageId'];
    }
    else{
        $languageId='1';
    }
    if($_REQUEST['mode']=='publisher_statistics'){
        $today=date("Y-m-d");
        if($_REQUEST['ad_site']!='' && $_REQUEST['ad_type']=='' && $_REQUEST['ad_status']==''){
            $sql_details="SELECT * FROM advertise_master AM ,advertise_details AD , adspace_master ADM ,adspace_details ASD , adsite_master SM where AM.advertiseId= AD.advertiseId and AD.adspaceId=ADM.adspaceId and ADM.adspaceId=ASD.adspaceId and AD.adsiteId=SM.adsiteId and  AM.siteuserId = '".$_SESSION['siteuserId']."' and AM.startDate > '".$_REQUEST['start_date']."' AND AM.endDate < '".$_REQUEST['end_date']."' and SM.adSiteUrl = '".$_REQUEST['ad_site']."'";
        }
        else if($_REQUEST['ad_site']=='' && $_REQUEST['ad_status']=='' && $_REQUEST['ad_type']!=''){
            $sql_details="SELECT * FROM advertise_master AM ,advertise_details AD , adspace_master ADM ,adspace_details ASD , adsite_master SM where AM.advertiseId= AD.advertiseId and AD.adspaceId=ADM.adspaceId and ADM.adspaceId=ASD.adspaceId and AD.adsiteId=SM.adsiteId and  AM.siteuserId = '".$_SESSION['siteuserId']."' and AM.startDate > '".$_REQUEST['start_date']."' AND AM.endDate < '".$_REQUEST['end_date']."' and ASD.adspaceType = '".$_REQUEST['ad_type']."'";
        }
        else if($_REQUEST['ad_site']=='' && $_REQUEST['ad_type']=='' && $_REQUEST['ad_status']!=''){
            if($_REQUEST['ad_status']=='ending'){
                $sql_details="SELECT * FROM advertise_master AM ,advertise_details AD , adspace_master ADM ,adspace_details ASD , adsite_master SM where AM.advertiseId= AD.advertiseId and AD.adspaceId=ADM.adspaceId and ADM.adspaceId=ASD.adspaceId and AD.adsiteId=SM.adsiteId and  AM.siteuserId = '".$_SESSION['siteuserId']."' and  AM.endDate = '".$today."'";
            }
            else if($_REQUEST['ad_status']=='expired'){
                $sql_details="SELECT * FROM advertise_master AM ,advertise_details AD , adspace_master ADM ,adspace_details ASD , adsite_master SM where AM.advertiseId= AD.advertiseId and AD.adspaceId=ADM.adspaceId and ADM.adspaceId=ASD.adspaceId and AD.adsiteId=SM.adsiteId and  AM.siteuserId = '".$_SESSION['siteuserId']."' and  AM.endDate < '".$today."'";
            }
        }
        elseif($_REQUEST['main'] == 'main' && $_REQUEST['ad_status']=='expired'){
            $sql_details="SELECT * FROM advertise_master AM ,advertise_details AD , adspace_master ADM ,adspace_details ASD , adsite_master SM where AM.advertiseId= AD.advertiseId and AD.adspaceId=ADM.adspaceId and ADM.adspaceId=ASD.adspaceId and AD.adsiteId=SM.adsiteId and  AM.siteuserId = '".$_SESSION['siteuserId']."' and  AM.endDate < '".$today."'";
        }
        else if($_REQUEST['ad_site']!='' && $_REQUEST['ad_type']!='' && $_REQUEST['ad_status']==''){
            $sql_details="SELECT * FROM advertise_master AM ,advertise_details AD , adspace_master ADM ,adspace_details ASD , adsite_master SM where AM.advertiseId= AD.advertiseId and AD.adspaceId=ADM.adspaceId and ADM.adspaceId=ASD.adspaceId and AD.adsiteId=SM.adsiteId and  AM.siteuserId = '".$_SESSION['siteuserId']."' and AM.startDate > '".$_REQUEST['start_date']."' AND AM.endDate < '".$_REQUEST['end_date']."' and SM.adSiteUrl = '".$_REQUEST['ad_site']."' AND ASD.adspaceType = '".$_REQUEST['ad_type']."'";
        }
        else if($_REQUEST['ad_site']=='' && $_REQUEST['ad_type']!='' && $_REQUEST['ad_status']!=''){
            if($_REQUEST['ad_status']=='ending'){
                $sql_details="SELECT * FROM advertise_master AM ,advertise_details AD , adspace_master ADM ,adspace_details ASD , adsite_master SM where AM.advertiseId= AD.advertiseId and AD.adspaceId=ADM.adspaceId and ADM.adspaceId=ASD.adspaceId and AD.adsiteId=SM.adsiteId and  AM.siteuserId = '".$_SESSION['siteuserId']."' AND AM.endDate = '".$today."' AND ASD.adspaceType = '".$_REQUEST['ad_type']."'";
            }
            else if($_REQUEST['ad_status']=='expired'){
                $sql_details="SELECT * FROM advertise_master AM ,advertise_details AD , adspace_master ADM ,adspace_details ASD , adsite_master SM where AM.advertiseId= AD.advertiseId and AD.adspaceId=ADM.adspaceId and ADM.adspaceId=ASD.adspaceId and AD.adsiteId=SM.adsiteId and  AM.siteuserId = '".$_SESSION['siteuserId']."' AND AM.endDate < '".$today."' AND ASD.adspaceType = '".$_REQUEST['ad_type']."'";
            }
        }
        else if($_REQUEST['ad_site']!='' && $_REQUEST['ad_type']=='' && $_REQUEST['ad_status']!=''){
            if($_REQUEST['ad_status']=='ending'){
                $sql_details="SELECT * FROM advertise_master AM ,advertise_details AD , adspace_master ADM ,adspace_details ASD , adsite_master SM where AM.advertiseId= AD.advertiseId and AD.adspaceId=ADM.adspaceId and ADM.adspaceId=ASD.adspaceId and AD.adsiteId=SM.adsiteId and  AM.siteuserId = '".$_SESSION['siteuserId']."' AND AM.endDate = '".$today."' AND SM.adSiteUrl = '".$_REQUEST['ad_site']."'";
            }
            else if($_REQUEST['ad_status']=='expired'){
                $sql_details="SELECT * FROM advertise_master AM ,advertise_details AD , adspace_master ADM ,adspace_details ASD , adsite_master SM where AM.advertiseId= AD.advertiseId and AD.adspaceId=ADM.adspaceId and ADM.adspaceId=ASD.adspaceId and AD.adsiteId=SM.adsiteId and  AM.siteuserId = '".$_SESSION['siteuserId']."' AND AM.endDate < '".$today."' AND SM.adSiteUrl = '".$_REQUEST['ad_site']."'";
            }
        }
        else if($_REQUEST['ad_site']!='' && $_REQUEST['ad_type']!='' && $_REQUEST['ad_status']!=''){
            if($_REQUEST['ad_status']=='ending'){
                $sql_details="SELECT * FROM advertise_master AM ,advertise_details AD , adspace_master ADM ,adspace_details ASD , adsite_master SM where AM.advertiseId= AD.advertiseId and AD.adspaceId=ADM.adspaceId and ADM.adspaceId=ASD.adspaceId and AD.adsiteId=SM.adsiteId and  AM.siteuserId = '".$_SESSION['siteuserId']."' AND AM.endDate = '".$today."' AND SM.adSiteUrl = '".$_REQUEST['ad_site']."' and ASD.adspaceType = '".$_REQUEST['ad_type']."'";
            }
            else if($_REQUEST['ad_status']=='expired'){
                $sql_details="SELECT * FROM advertise_master AM ,advertise_details AD , adspace_master ADM ,adspace_details ASD , adsite_master SM where AM.advertiseId= AD.advertiseId and AD.adspaceId=ADM.adspaceId and ADM.adspaceId=ASD.adspaceId and AD.adsiteId=SM.adsiteId and  AM.siteuserId = '".$_SESSION['siteuserId']."' AND AM.endDate < '".$today."' AND SM.adSiteUrl = '".$_REQUEST['ad_site']."' and ASD.adspaceType = '".$_REQUEST['ad_type']."'";
            }
        }
        else if($_REQUEST['purchase_type']!='' && $_REQUEST['main1']=='main1'){
        // 	if($_REQUEST['purchase_type']=='direct')
        // 	{
                $sql_details="SELECT * FROM advertise_master AM ,advertise_details AD , adspace_master ADM ,adspace_details ASD , adsite_master SM where AM.advertiseId= AD.advertiseId and AD.adspaceId=ADM.adspaceId and ADM.adspaceId=ASD.adspaceId and AD.adsiteId=SM.adsiteId and  AM.siteuserId = '".$_SESSION['siteuserId']."'";
        // 	}
        // 	if($_REQUEST['purchase_type']=='network')
        // 	{
        // 		$sql_details="SELECT * FROM advertise_master AM ,advertise_details AD , adspace_master ADM ,adspace_details ASD , adsite_master SM where AM.advertiseId= AD.advertiseId and AD.adspaceId=ADM.adspaceId and ADM.adspaceId=ASD.adspaceId and AD.adsiteId=SM.adsiteId and  AM.siteuserId = '".$_SESSION['siteuserId']."'";
        // 	}
        }
        else{
            $sql_details="SELECT * FROM advertise_master AM ,advertise_details AD , adspace_master ADM ,adspace_details ASD , adsite_master SM where AM.advertiseId= AD.advertiseId and AD.adspaceId=ADM.adspaceId and ADM.adspaceId=ASD.adspaceId and AD.adsiteId=SM.adsiteId and  AM.siteuserId = '".$_SESSION['siteuserId']."' and AM.startDate > '".$_REQUEST['start_date']."' AND AM.endDate < '".$_REQUEST['end_date']."'";
        }
        if($_REQUEST['purchase_type']!=''){
            if($_REQUEST['purchase_type']=='direct'){
                $sql_details.= "and AD.networkId = 0";
            }
            if($_REQUEST['purchase_type']=='network'){
                $sql_details.= "and AD.networkId > 0";
            }
        }
    }
    else{
     $sql_details="SELECT * FROM advertise_master AM ,advertise_details AD , adspace_master ADM ,adspace_details ASD , adsite_master SM where AM.advertiseId= AD.advertiseId and AD.adspaceId=ADM.adspaceId and ADM.adspaceId=ASD.adspaceId and AD.adsiteId=SM.adsiteId and AM.siteuserId = '".$_SESSION['siteuserId']."'";
    }
    $query_details=mysql_query($sql_details);
    $num=mysql_num_rows($query_details);
?>
<link rel="stylesheet" href="css/xtheme-gray.css" type="text/css" />
<link rel="stylesheet" href="css/styles.css" type="text/css" />
<link rel="stylesheet" href="css/ext-all.css" type="text/css" />
<script type="text/javascript">
function search_pub_statistics(){
    document.adspaces.mode.value="publisher_statistics";
    document.adspaces.submit();
}
</script>
<!--<SCRIPT language=javascript src="calender/js/popcalendar.js"></SCRIPT>-->
<script language="javascript" type="text/javascript" src="JavaScripts/datetimepicker.js"></script>
<form name="adspaces" action="" method="POST" id="adspaces" onsubmit="return search_pub_statistics();">
<input type="hidden" name="mode" value=""><br />
<br />
<table width="90%" border="0" cellspacing="0" cellpadding="0" align="center">
<tr>
    <td valign="top" align="center">
    <table width="98%" border="0" align="center" cellpadding="0" cellspacing="0" >
    <tr>
        <td>&nbsp;</td>
        <td>
        <table width="100%" border="0" cellspacing="0" cellpadding="0" align="center">
        <tr>
            <td>
            <table width="100%" border="0" cellspacing="0" cellpadding="0">
            <tr>
                <td width="11"><img src="images/PinkBorderTopLeft.jpg" alt="" width="11" height="11"></td>
                <td class="PinkBoxTopBg"><img src="images/spacer.gif" alt="" width="10" height="11"></td>
                <td width="11"><img src="images/PinkBorderTopRight.jpg" alt="" width="11" height="11"></td>
            </tr>
            </table>
            </td>
        </tr>
        <tr>
            <td class="PinkTwosideBorder">
            <table width="98%" border="0" align="center" cellpadding="0" cellspacing="0">
            <body id="tab1">
            <ul id="tabnav">
                <li class="tab1"><a href="advertiser_statistics.php">Today</a></li>
                <li class="tab2"><a href="advertiser_custom_statistics.php">Custom</a></li>
            </ul>
            </body>
        <?php
            $Current_date=date('Y-m-d');
        ?>
        <tr>
            <td class="BoxContent">
            <!-- #########starts design ######### -->
            <div class="thirdNavBoxMid">
                <div class="statsPageTitle">Live Dashboard: <?php echo $Current_date; ?></div>
                <?php
                $select_user_ads="select advertiseId, adTitle from advertise_master where siteuserId = '".$_SESSION[siteuserId]."' and adTitle<>''";
                $exe_user_ads=mysql_query($select_user_ads);
                ?>
                <div style="float: left; width: 312px;">
                    <div>
                    <form name="frmAdSelector" action="" method="post">
                    <div style="width: 78px; float: left;" class="mediumLargeGreyText" align="left">Select Ad:</div>
                    <div style="float: left;" class="arial12">
                    <select style="font-weight: normal; font-size: 12px;" name="drpAds" id="drpAds" onchange="this.form.submit();">
                        <option label="-- All Ads --" value="all" selected="selected">-- All Ads --</option>
                        <?php
                        while($row_user_ads=mysql_fetch_array($exe_user_ads)){
                        ?>
                        <option label="<?php echo $row_user_ads['adTitle']; ?>" value="<?php echo $row_user_ads['advertiseId']; ?>"><?php echo $row_user_ads['adTitle']; ?></option>
                        <?php
                        }
                        ?>
                    </select>
                    </div>
                    </form>
                    <div style="clear: both;"></div>
                    </div>
                    <br>
                <table id="statsTable" class="statsTable" width="100%" cellspacing="0">
		<tbody>
                <tr id="ImpsRow" class="active" onclick='MB.util.stats.getChart("column", "http://www.admob.com/ajax/stats_charts/", "chartType=adImps&amp;id=all", "theChart", this);'>
                    <td class="label" style="border-top: 1px dashed rgb(178, 178, 178);">
                    <span class="largeGreyTextBold">Impressions:</span>
                    </td>
                    <td class="number" style="border-top: 1px dashed rgb(178, 178, 178);">
                        <span class="largeGreenTextBold">0</span>
                    </td>
                </tr>
		<tr class="" id="ClicksRow" onclick='MB.util.stats.getChart("column", "http://www.admob.com/ajax/stats_charts/", "chartType=adClicks&amp;id=all", "theChart", this);'>
                    <td class="label">
                        <span class="largeGreyTextBold">Clicks:</span>
                    </td>
                    <td class="number">
                        <span class="largeGreenTextBold">0</span>
                    </td>
		</tr>
		<tr class="" id="CTRRow" onclick='MB.util.stats.getChart("column", "http://www.admob.com/ajax/stats_charts/", "chartType=adCTR&amp;id=all", "theChart", this);'>
                    <td class="label">
                        <span class="largeGreyTextBold">CTR:</span>
                    </td>
                    <td class="number">
                        <span style="visibility: visible;" id="ext-gen14" class="largeGreenTextBold">0.00%</span>
                    </td>
		</tr>
		<tr class="" id="CPCRow" onclick='MB.util.stats.getChart("column", "http://www.admob.com/ajax/stats_charts/", "chartType=adCPC&amp;id=all", "theChart", this);'>
                    <td class="label">
                        <span class="largeGreyTextBold">Average CPC:</span>
                    </td>
                    <td class="number">
                        <span style="visibility: visible;" id="ext-gen15" class="largeGreenTextBold">$0.00</span>
                    </td>
		</tr>
		<tr class="" id="CostRow" onclick='MB.util.stats.getChart("column", "http://www.admob.com/ajax/stats_charts/", "chartType=adCost&amp;id=all", "theChart", this);'>
                    <td class="label">
                        <span class="largeGreyTextBold">Cost:</span>
                    </td>
                    <td class="number">
                        <span class="largeGreenTextBold">$0.00</span>
                    </td>
		</tr>
                </tbody>
                </table>
                    <div class="smallGreyText" style="padding-top: 5px;">Dashboard stats are updated automatically every 15 seconds.<br>All Dates GMT.</div>
                </div>
            </div>
            <!-- ##########ends design ######## -->

            <!-- ########### right sidedesign starts ############# -->
            <div style="float: left; width: 439px;">
                <div style="padding: 0px 10px 10px 30px;">
                    <img id="chartSpinner" style="visibility: hidden;" src="images/spinner.gif" alt="">
                    <div id="theChart"><!--chart--></div>
                </div>
            </div>
            <!-- ############ right side design ends ############ -->
            </td>
        </tr>
        </table>
        </td>
    </tr>
    <tr>
        <td>
        <table width="100%" border="0" cellspacing="0" cellpadding="0">
        <tr>
            <td width="11"><img src="images/PinkBorderBottomLeft.jpg" alt="" width="11" height="11"></td>
            <td class="PinkBoxBottomBg"><img src="images/spacer.gif" alt="" width="10" height="11"></td>
            <td width="11"><img src="images/PinkBorderBottomRight.jpg" alt="" width="11" height="11"></td>
        </tr>
        </table>
        </td>
    </tr>
    </table>
    </td>
    <td>&nbsp;</td>
    </tr>
    </table>
    </td>
</tr>
</table>
</form>            
<?php
}
?>
